<template>
  <div>
    <el-row :gutter="15">
      <el-form ref="formData" :model="formData" :rules="rules" size="medium" label-width="100px">
        <el-col :span="14">
          <el-form-item label="需要保留的天数" prop="days" label-width="120px">
            <el-input v-model="formData.days" placeholder="需要保留的天数" :style="{width: '30%'}"></el-input>
            <el-button style="margin-left: 20px;" type="primary" @click="submitForm">提交</el-button>
          </el-form-item>
        </el-col>
        <!-- <el-col :span="24">
          <el-form-item size="large">
            <el-button type="primary" @click="submitForm">提交</el-button>
          </el-form-item>
        </el-col> -->
      </el-form>
    </el-row>
  </div>
</template>
<script>
import { getDeleteDays,updateDays } from "@/api/his/deleteTask";

export default {
  name: 'delete',
  data() {
    return {
      formData: {
        days: null,
        id: null,
      },
      rules: {
        days: [
          { required: true, message: '请输入需要保留的天数', trigger: 'blur' },
          { validator: this.validatePositiveInteger, trigger: 'blur' }
        ]
      }
    }
  },
  mounted() {
    this.getDays();
  },
  methods: {
    validatePositiveInteger(rule, value, callback) {
      if (value && !/^\d+$/.test(value)) {
        callback(new Error('请输入正整数'));
      } else {
        callback();
      }
    },
    getDays() {
      getDeleteDays().then(response => {
        if (response && response.length > 0 && response[0].days) {
          this.formData.days = parseInt(response[0].days, 10); // 确保days是整数类型
          this.formData.id = response[0].id;
        }
      }).catch(error => {
        console.error('获取天数失败', error);
        // 处理错误情况，比如显示错误信息给用户
      });
    },
    submitForm() {
      this.$refs.formData.validate((valid) => {
        if (valid) {
          // 表单验证通过，执行提交操作
          updateDays(this.formData).then(response => {
            this.$message.success('修改成功！');
            this.getDays();
          }).catch(error => {
            this.$message.error('系统繁忙，请稍后重试！');
          });
        } else {
          this.$message.error('表单验证失败，请检查输入！');
        }
      });
    }
  }
}
</script>
<style scoped lang="scss">
</style>
